{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "07775ba6-b681-4999-9c3f-ffa2b7cf9b8f",
   "metadata": {},
   "source": [
    "# 一、5种数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f9b15106-6e05-4124-975b-45a138afd929",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8b66b5d-a66b-4576-a0a9-d97d0678dcba",
   "metadata": {},
   "source": [
    "## （1）四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d174c97f-3c8d-4064-a090-01e49d9b29fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 = np.arange(1, 13).reshape(3, 4)\n",
    "n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fbd54dc2-ac84-45f8-a4b7-c71863ff52e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[18, 17, 11, 15],\n",
       "       [17, 13, 12, 14],\n",
       "       [18, 10, 13, 17]], dtype=int32)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2 = np.random.randint(10, 20, size=(3, 4))\n",
    "n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b4fb4d39-1ee6-43bf-800f-cbaf161c78bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19, 19, 14, 19],\n",
       "       [22, 19, 19, 22],\n",
       "       [27, 20, 24, 29]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 + n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd1fb919-4c07-429b-8b50-e9b9ca6a7506",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17, 15,  8, 11],\n",
       "       [12,  7,  5,  6],\n",
       "       [ 9,  0,  2,  5]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2 - n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f044c77e-b62b-4580-a1bf-c077323e95c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 18,  34,  33,  60],\n",
       "       [ 85,  78,  84, 112],\n",
       "       [162, 100, 143, 204]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 * n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "870a1e8e-ee99-4f80-8c56-9520c529b9f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.05555556, 0.11764706, 0.27272727, 0.26666667],\n",
       "       [0.29411765, 0.46153846, 0.58333333, 0.57142857],\n",
       "       [0.5       , 1.        , 0.84615385, 0.70588235]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 / n2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "064cd358-58d5-43b3-82e9-78d0b7bed252",
   "metadata": {},
   "source": [
    "## （2）幂运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "19355c7e-9777-4e08-ab0e-3419dd6e4247",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 = np.linspace(1, 10, num=10, dtype='int')\n",
    "n3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "34938d2d-2e20-4cbd-9e4f-5ac3eb61530b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   4,   9,  16,  25,  36,  49,  64,  81, 100])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8a39e630-3f78-48d9-9c53-611e2f73503d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[                  1,              131072,              177147,\n",
       "                 1073741824],\n",
       "       [       762939453125,         13060694016,         13841287201,\n",
       "              4398046511104],\n",
       "       [ 150094635296999121,         10000000000,      34522712143931,\n",
       "        2218611106740436992]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 ** n2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51d462d3-8068-4022-afa8-bf53a4a25d67",
   "metadata": {},
   "source": [
    "## （3）比较运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "faa6ef56-4b95-4eaf-b5e7-27c49d76db2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False,  True,  True,  True,  True,\n",
       "        True])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 > 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d6c193d-01e0-43fd-8809-6f9b781465e1",
   "metadata": {},
   "source": [
    "## （4）标量运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c19ba9f5-2cc5-4a0a-a337-67c55ace0e5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  4,  6,  8, 10, 12, 14, 16, 18, 20])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c6749d8c-ff71-40ff-b542-ad9d594eff92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2721a1d6-a8be-493a-9dfb-039fb661c2b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "70424e07-4eb4-4ba8-b2c4-ecce49000349",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1,  0,  1,  2,  3,  4,  5,  6,  7,  8])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 - 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2221b95-ddff-42e7-b5d3-8267c60de9a9",
   "metadata": {},
   "source": [
    "## （5）矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a1c7e850-05d5-4b2a-9d32-19931b1e7b2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bb8efd8f-c642-4273-842c-72e3e82d8c5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "n4 = np.logspace(0, 11, num=12, base=2, dtype='int').reshape(4, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "cc6406d2-9f94-45c3-ae23-70764c1f6f96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   1,    2,    4],\n",
       "       [   8,   16,   32],\n",
       "       [  64,  128,  256],\n",
       "       [ 512, 1024, 2048]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0bb11e0d-b622-44f7-aacc-f61a9e4f0c5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 3)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n4.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9d762d03-43de-475b-8e63-22db698674e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2257,  4514,  9028],\n",
       "       [ 4597,  9194, 18388],\n",
       "       [ 6937, 13874, 27748]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(n1, n4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4e45dd0d-eae9-4a2b-b49a-a50a46c3ee8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   47,    54,    61,    68],\n",
       "       [  376,   432,   488,   544],\n",
       "       [ 3008,  3456,  3904,  4352],\n",
       "       [24064, 27648, 31232, 34816]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(n4, n1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2f1d7e15-c223-4300-b9ac-fe54eeaa9b66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2257,  4514,  9028],\n",
       "       [ 4597,  9194, 18388],\n",
       "       [ 6937, 13874, 27748]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 @ n4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37769982-a9a3-4c27-8ac3-1fb9292e47b9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
